using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;


using Material.Object;
using Material.DAL;
namespace Material.Entity
{
	public class TblTransHutangDTLEntity
	{
		private Accessor accessor;
		private string connString;

		public TblTransHutangDTLEntity (string _connString)
		{
			 this.connString = _connString;
		}

		public int Insert(TblTransHutangDTL tbltranshutangdtl)
		{
			 int result = -1;
			 accessor = new Accessor(true, connString);
			 accessor.CommandText = "TblTransHutangDTL_Insert";
			 accessor.CommandType = CommandType.StoredProcedure;
			 //accessor.Add("@IdTransDTL" ,tbltranshutangdtl.IdTransDTL );
			 accessor.Add("@IdTransHDR" ,tbltranshutangdtl.IdTransHDR );
			 accessor.Add("@TglBayar" ,tbltranshutangdtl.TglBayar );
			 accessor.Add("@JumlahBayar" ,tbltranshutangdtl.JumlahBayar );
			 accessor.Add("@CreatedDate" ,tbltranshutangdtl.CreatedDate );
			 accessor.Add("@CreatedBy" ,tbltranshutangdtl.CreatedBy );
			 accessor.Add("@ModifiedDate" ,tbltranshutangdtl.ModifiedDate );
			 accessor.Add("@ModifiedBy" ,tbltranshutangdtl.ModifiedBy );

			 result = Excutor.ExecuteNonQuery(accessor);
			 if (result != -1)
				 accessor.CommitTransaction();
			 else
				 accessor.RollBackTransaction();
			 return result;
		}

		public int Update(TblTransHutangDTL tbltranshutangdtl)
		{
			 int result = -1;
			 accessor = new Accessor(true, connString);
			 accessor.CommandText = "TblTransHutangDTL_Update";
			 accessor.CommandType = CommandType.StoredProcedure;
			 accessor.Add(@"IdTransDTL" ,tbltranshutangdtl.IdTransDTL );
			 accessor.Add(@"IdTransHDR" ,tbltranshutangdtl.IdTransHDR );
			 accessor.Add(@"TglBayar" ,tbltranshutangdtl.TglBayar );
			 accessor.Add(@"JumlahBayar" ,tbltranshutangdtl.JumlahBayar );
             //accessor.Add(@"CreatedDate" ,tbltranshutangdtl.CreatedDate );
             //accessor.Add(@"CreatedBy" ,tbltranshutangdtl.CreatedBy );
			 accessor.Add(@"ModifiedDate" ,tbltranshutangdtl.ModifiedDate );
			 accessor.Add(@"ModifiedBy" ,tbltranshutangdtl.ModifiedBy );

			 result = Excutor.ExecuteNonQuery(accessor);
			 if (result != -1)
				 accessor.CommitTransaction();
			 else
				 accessor.RollBackTransaction();
			 return result;
		}

		public int Delete(TblTransHutangDTL tbltranshutangdtl)
		{
			 int result = -1;
			 accessor = new Accessor(true, connString);
			 accessor.CommandText = "TblTransHutangDTL_Delete";
			 accessor.CommandType = CommandType.StoredProcedure;
			 accessor.Add(@"IdTransDTL" ,tbltranshutangdtl.IdTransDTL);

			 result = Excutor.ExecuteNonQuery(accessor);
			 if (result != -1)
				 accessor.CommitTransaction();
			 else
				 accessor.RollBackTransaction();
			 return result;
		}

        public int DeleteByIdTransHDR(TblTransHutangDTL tbltranshutangdtl)
        {
            int result = -1;
            accessor = new Accessor(true, connString);
            accessor.CommandText = "TblTransHutangDTL_DeleteByIdTransHDR";
            accessor.CommandType = CommandType.StoredProcedure;
            accessor.Add(@"IdTransHDR", tbltranshutangdtl.IdTransHDR);

            result = Excutor.ExecuteNonQuery(accessor);
            if (result != -1)
                accessor.CommitTransaction();
            else
                accessor.RollBackTransaction();
            return result;
        }

		public TblTransHutangDTL GetDataByID(TblTransHutangDTL tbltranshutangdtl)
		{
			 TblTransHutangDTL result = new TblTransHutangDTL();
			 accessor = new Accessor(true, connString);
			 accessor.CommandText = "TblTransHutangDTL_SelectByID";
			 accessor.CommandType = CommandType.StoredProcedure;
			 accessor.Add(@"IdTransDTL" ,tbltranshutangdtl.IdTransDTL);

			 SqlDataReader rdr = Excutor.GetDataReader(accessor);
			 if (rdr.HasRows)
			 {
				 while (rdr.Read())
					 {
						 result.IdTransDTL = rdr.GetInt32(0);
						 result.IdTransHDR = rdr.GetInt32(1);
						 result.TglBayar = rdr.GetDateTime(2);
						 result.JumlahBayar = rdr.GetDecimal(3);
						 result.CreatedDate = rdr.GetDateTime(4);
						 result.CreatedBy = rdr.GetString(5);
						 result.ModifiedDate = rdr.GetDateTime(6);
						 result.ModifiedBy = rdr.GetString(7);
					  }
			}
			 return result;
		}

		public List<TblTransHutangDTL> GetDataAll()
		{
			 List<TblTransHutangDTL> result = new List<TblTransHutangDTL>();
			 accessor = new Accessor(true, connString);
			 accessor.CommandText = "TblTransHutangDTL_SelectAll";
			 accessor.CommandType = CommandType.StoredProcedure;

			 SqlDataReader rdr = Excutor.GetDataReader(accessor);
			 if (rdr.HasRows)
			 {
				 while (rdr.Read())
					 {
						 TblTransHutangDTL obj = new TblTransHutangDTL();
						 obj.IdTransDTL = rdr.GetInt32(0);
						 obj.IdTransHDR = rdr.GetInt32(1);
						 obj.TglBayar = rdr.GetDateTime(2);
						 obj.JumlahBayar = rdr.GetDecimal(3);
						 obj.CreatedDate = rdr.GetDateTime(4);
						 obj.CreatedBy = rdr.GetString(5);
						 obj.ModifiedDate = rdr.GetDateTime(6);
						 obj.ModifiedBy = rdr.GetString(7);
						 result.Add(obj);
					  }
			}
			 return result;
		}

        public List<TblTransHutangDTL> GetDataByIdHDR(TblTransHutangDTL tbltranshutangdtl)
        {
            List<TblTransHutangDTL> result = new List<TblTransHutangDTL>();
            accessor = new Accessor(true, connString);
            accessor.CommandText = "TblTransHutangDTL_SelectByIdHDR";
            accessor.CommandType = CommandType.StoredProcedure;
            accessor.Add(@"IdTransHDR", tbltranshutangdtl.IdTransHDR);
            SqlDataReader rdr = Excutor.GetDataReader(accessor);
            if (rdr.HasRows)
            {
                while (rdr.Read())
                {
                    TblTransHutangDTL obj = new TblTransHutangDTL();
                    obj.IdTransDTL = rdr.GetInt32(0);
                    obj.IdTransHDR = rdr.GetInt32(1);
                    obj.TglBayar = rdr.GetDateTime(2);
                    obj.JumlahBayar = rdr.GetDecimal(3);
                    obj.CreatedDate = rdr.GetDateTime(4);
                    obj.CreatedBy = rdr.GetString(5);
                    obj.ModifiedDate = rdr.GetDateTime(6);
                    obj.ModifiedBy = rdr.GetString(7);
                    result.Add(obj);
                }
            }
            return result;
        }

        public DataSet GetDataDSByIdHDR(string IdTransHDR)
        {
            StringBuilder s = new StringBuilder();

            s.AppendLine("Select 		IdTransDTL	AS	'IdTransDTL',");
            s.AppendLine("IdTransHDR	AS	'IdTransHDR',");
            s.AppendLine("CONVERT(VARCHAR,TglBayar,113)	AS	'TglBayar',");
            s.AppendLine("JumlahBayar	AS	'JumlahBayar',");
            s.AppendLine("CreatedDate	AS	'CreatedDate',");
            s.AppendLine("CreatedBy	AS	'CreatedBy',");
            s.AppendLine("ModifiedDate	AS	'ModifiedDate',");
            s.AppendLine("ModifiedBy	AS	'ModifiedBy'");

            s.AppendLine("From	TblTransHutangDTL");
            s.AppendLine("WHERE IdTransHDR ="+IdTransHDR);
            s.AppendLine("ORDER BY TglBayar DESC");
            DataSet result = new DataSet();
            accessor = new Accessor(true, connString);
            accessor.CommandText = s.ToString();// "TblTransHutangDTL_SelectByIdHDR";
            accessor.CommandType = CommandType.Text;
            //accessor.Add(@"IdTransHDR", tbltranshutangdtl.IdTransHDR);
            result = Excutor.GetDataSet(accessor);

            return result;
        }

        public SqlCommand GetCommandReport(TblTransHutangDTL tbltranshutangdtl)
        {
            SqlCommand comm = new SqlCommand();

            accessor = new Accessor(true, connString);
            accessor.CommandText = "TblTransHutangHDR_ReportHutangDetail";
            accessor.CommandType = CommandType.StoredProcedure;
            accessor.Add(@"IdTransHDR", tbltranshutangdtl.IdTransHDR);
            comm = accessor.Command;

            return comm;
        }


	}
}
